﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.WarehouseManage;
using SmartWMS.EntityFramework;
using SmartWMS.Server.BasicsDatumServer.SupplierManagement;
using SmartWMS.Server.Repository;
using SmartWMS.Server.SystemSets.RoleServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SmartWMS.Server.BasicsDatumServer.SupplierManagement
{
    /// <summary>
    /// 供应商管理方法
    /// </summary>
    public class SupplierManagement : ISupplierManagement
    {
        private readonly IRepository<Supplier> _supplier;

        private readonly SmartWMSContext _smartWMSContext;

        public SupplierManagement(SmartWMSContext smartWMSContext, IRepository<Supplier> supplier)
        {
            _smartWMSContext = smartWMSContext;
            _supplier = supplier;
        }

        /// <summary>
        /// 批量删除供应商
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> DeletesSupplierManagementIsDisable(SupplierManagementDto dto)
        {
            var sid = dto.DelId.Split(',');
            var list=new List<Supplier>();
            foreach (var item in sid)
            {
                var SuppList= _supplier.GetListQuery().FirstOrDefault(m => m.Id == Convert.ToInt32(item));
                list.Add(SuppList);
            }
            var result = _supplier.DeleteAsync(list);
            return result.Result>0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));
        }

        /// <summary>
        /// 供应商管理删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeleteSupplierManagementDtoAsyne(SupplierManagementDto dto)
        {
            //删除
            var info = _supplier.GetListQuery().FirstOrDefault(m => m.Id == dto.id);
            var result = await _supplier.DeleteByIdAsync(info);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }


        /// <summary>
        /// 供应商管理反填
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<SupplierManagementDto> GetSupplierManagementById(SupplierManagementDto dto)
        {
            //反填
            var info = _supplier.GetListQuery().FirstOrDefault(m => m.Id == dto.id);
            var result = info.MapTo<SupplierManagementDto>();
            return Response<SupplierManagementDto>.Ok(result);
        }



        /// <summary>
        /// 供应商管理列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<dynamic> GetSupplierManagementDtoDtoAsyne(SupplierFyDto supplierFyDto)
        {
            var list = _supplier.GetListQuery();

            if (!string.IsNullOrEmpty(supplierFyDto.SupplierCode))
            {
                list = list.Where(x => x.SupplierCode.Contains(supplierFyDto.SupplierCode));
            }
            if (!string.IsNullOrEmpty(supplierFyDto.SupplierName))
            {
                list = list.Where(x => x.SupplierName.Contains(supplierFyDto.SupplierName));
            }
            if (!string.IsNullOrEmpty(supplierFyDto.Phone))
            {
                list = list.Where(x => x.Phone.Contains(supplierFyDto.Phone));
            }

            //分页
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / supplierFyDto.pageSize);
            list = list.Skip((supplierFyDto.pageIndex - 1) * supplierFyDto.pageSize).Take(supplierFyDto.pageSize);


            var result = list.MapToList<SupplierManagementDto>();
            return Response<dynamic>.Ok(new
            {
                totalCount,
                pageCount,
                result
            });
        }

        /// <summary>
        /// 供应商管理添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> SupplierManagementDtoAddSave(SupplierManagementDto dto)
        {
            //添加
            var info = dto.MapTo<Supplier>();
            var result =await _supplier.InsertAsync(info);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }

        /// <summary>
        /// 供应商管理修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> SupplierManagementUpdate(SupplierManagementDto dto)
        {
            //修改
            var info = dto.MapTo<Supplier>();
            var result =await _supplier.UpdateAsync(info);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }

    }
}
